package com.icss.orders.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.icss.common.service.BaseServiceImpl;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.common.vo.JSONEnum;
import com.icss.goods.service.IGoodsDbService;
import com.icss.goods.vo.ReGoods;
import com.icss.orders.dao.IReOrdersDao;
import com.icss.orders.dao.IReOrdersGoodsDao;
import com.icss.orders.service.IOrdersDbService;
import com.icss.orders.vo.ReOrders;
import com.icss.orders.vo.ReOrdersGoods;
import com.icss.users.service.IUsersDbService;
import com.icss.users.vo.ReCart;
import com.icss.users.vo.ReCartEnum;
import com.icss.users.vo.ReUsers;

/**
 * Service实现类(订单代码)
 * 
 * @author 刘鑫
 */
@Service("ordersDbService")
public class OrdersDbServiceImplay extends BaseServiceImpl implements IOrdersDbService
{
	@Resource
	private IReOrdersDao ordersDao;
	@Resource
	private IReOrdersGoodsDao ordersGoodsDao;
	
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private IGoodsDbService goodsDbService;
	
	@Override
	public ApiResponse<Object> saveOneOrdersService(ReOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 判断折扣 */
		/* 判断是否是会员 */
		condMap.put("id", orders.getUsersId());
		ApiResponse<ReUsers> usersApiResponse = this.usersDbService.findOneUsersService(condMap);
		ReUsers users = usersApiResponse.getDataOneJava() ;
		double discount = users.getDiscount() ; 
		orders.setDiscount(discount);
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		int res = this.ordersDao.save(orders);
		if (res > 0)
		{
			double totalPrice = 0 ; 
			/* 存储订单明细 */
			for (Iterator iterator = orders.getCartList().iterator(); iterator.hasNext();)
			{
				ReCart cart = (ReCart) iterator.next();
				ReGoods goods = cart.getGoods() ; 
				
				if(goods.getStockNum() < cart.getNum())
				{
					ConstatFinalUtil.SYS_LOGGER.info("==库存不够:{}==",goods.getId());
					continue ; 
				}
				
				ReOrdersGoods ordersGoods = new ReOrdersGoods();
				ordersGoods.setOrdersId(orders.getId());
				ordersGoods.setGoodsId(cart.getGoodsId());
				
				ordersGoods.setName(goods.getName());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setNum(cart.getNum());
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				/* 总价格相加 */
				totalPrice = totalPrice + ordersGoods.getTotalPrice() ; 
				
				ordersGoods.setStatus(orders.getStatus());
				ordersGoods.setPubTime(new Date());
				/* 订单商品 */
				this.saveOneOrdersGoodsService(ordersGoods);
				
				cart.setNum(0);
				cart.setStatus(ReCartEnum.STATUS_DISABLED.getStatus());
				cart.setUpdateTime(new Date());
				
				/* 清空购物车 */
				this.usersDbService.updateOneCartService(cart);
				
				/* 更新商品的销量 */
				goods.setSaleNum(goods.getSaleNum() + ordersGoods.getNum());
				goods.setStockNum(goods.getStockNum() - ordersGoods.getNum());
				goods.setUpdateTime(new Date());
				this.goodsDbService.updateOneGoodsService(goods);
			}
			
			/* 设置总价格 */
			orders.setTotalPrice(totalPrice);
			/* 应付金额,打折 */
			orders.setDealAmount(orders.getTotalPrice() * orders.getDiscount());
			/* 更新订单 */
			this.ordersDao.update(orders);
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersService(ReOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.update(orders);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ReOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ReOrders> apiResponse = new ApiResponse<ReOrders>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ReOrders> demoOrdersList = Collections.EMPTY_LIST;

		Map<String, List<ReOrders>> dataList = new HashMap<String, List<ReOrders>>();
		
		/* 对关键字进行模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
		}
		
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			demoOrdersList = this.ordersDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		}else
		{
			demoOrdersList = this.ordersDao.findList(condMap);
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ReOrders> findOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<ReOrders> apiResponse = new ApiResponse<ReOrders>();

		Map<String, ReOrders> data = new HashMap<String, ReOrders>();
		ReOrders orders = this.ordersDao.findOne(condMap);
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), orders);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(orders);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneOrdersGoodsService(ReOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		ordersGoods.setCreateTime(new Date());
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.save(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersGoodsService(ReOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.update(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersGoodsDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ReOrdersGoods> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ReOrdersGoods> apiResponse = new ApiResponse<ReOrdersGoods>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ReOrdersGoods> demoOrdersGoodsList = Collections.EMPTY_LIST;

		Map<String, List<ReOrdersGoods>> dataList = new HashMap<String, List<ReOrdersGoods>>();
		
		/* 对关键字进行模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
		}
		
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			demoOrdersGoodsList = this.ordersGoodsDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		}else
		{
			demoOrdersGoodsList = this.ordersGoodsDao.findList(condMap);
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersGoodsList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersGoodsList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ReOrdersGoods> findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<ReOrdersGoods> apiResponse = new ApiResponse<ReOrdersGoods>();

		Map<String, ReOrdersGoods> data = new HashMap<String, ReOrdersGoods>();
		ReOrdersGoods ordersGoods = this.ordersGoodsDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersGoods);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}
